package flc.ast.fragment.edit;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.widget.SeekBar;

import androidx.fragment.app.Fragment;

import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.ModuleConfig;
import com.stark.imgedit.fliter.PhotoProcessing;

import flc.ast.BaseEditFragment;
import flc.ast.R;
import flc.ast.databinding.FragmentBeautyBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BeautyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BeautyFragment extends BaseEditFragment<FragmentBeautyBinding> implements SeekBar.OnSeekBarChangeListener {

    public static final int INDEX = ModuleConfig.INDEX_BEAUTY;
    private Bitmap mImgBitmap, mSaveBmp;


    public static BeautyFragment newInstance() {
        BeautyFragment fragment = new BeautyFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mImgEditActivity.mode = ImgEditActivity.MODE_BEAUTY;
        clearSelection();
        mDataBinding.ivSkin.setVisibility(View.VISIBLE);
        mDataBinding.llSkin.setVisibility(View.VISIBLE);
        mDataBinding.sbSkin.setProgress(0);
        mDataBinding.sbSmooth.setProgress(0);
        mDataBinding.tvSkinNum.setText(0 + "");
        mDataBinding.tvSmoothNum.setText(0 + "");
        mImgBitmap = mImgEditActivity.getMainBit();
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = ImgEditActivity.MODE_NONE;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mImgEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_beauty;
    }

    @Override
    protected void initView() {
        mDataBinding.sbSmooth.setOnSeekBarChangeListener(this);
        mDataBinding.sbSkin.setOnSeekBarChangeListener(this);
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivApply.setOnClickListener(this);
        mDataBinding.tvSkin.setOnClickListener(this);
        mDataBinding.tvSmooth.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSkin://美白
                clearSelection();
                mDataBinding.tvSkin.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivSkin.setVisibility(View.VISIBLE);
                mDataBinding.llSkin.setVisibility(View.VISIBLE);
                break;
            case R.id.tvSmooth://磨皮
                clearSelection();
                mDataBinding.tvSmooth.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivSmooth.setVisibility(View.VISIBLE);
                mDataBinding.llSmooth.setVisibility(View.VISIBLE);
                break;
            case R.id.ivCancel://取消
                backToMain();
                break;
            case R.id.ivApply://确定
                applyBeautyImage();
                break;
        }
    }

    @Override
    protected void initData() {
        onShow();
    }

    /**
     * 保存美颜图片
     */
    private void applyBeautyImage() {
        mImgEditActivity.changeMainBitmap(mSaveBmp, true);
        backToMain();
    }

    private void clearSelection() {
        mDataBinding.tvSkin.setTextColor(Color.parseColor("#969696"));
        mDataBinding.tvSmooth.setTextColor(Color.parseColor("#969696"));
        mDataBinding.ivSkin.setVisibility(View.INVISIBLE);
        mDataBinding.ivSmooth.setVisibility(View.INVISIBLE);
        mDataBinding.llSkin.setVisibility(View.GONE);
        mDataBinding.llSmooth.setVisibility(View.GONE);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        doBeautyTask();
    }

    private void doBeautyTask() {
        final int smoothValue = mDataBinding.sbSmooth.getProgress();
        final int skinValue = mDataBinding.sbSkin.getProgress();
        mDataBinding.tvSmoothNum.setText(smoothValue + "");
        mDataBinding.tvSkinNum.setText(skinValue + "");
        showDialog(getString(com.stark.common.res.R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap bitmap = Bitmap.createBitmap(mImgBitmap.copy(
                        Bitmap.Config.ARGB_8888, true));
                PhotoProcessing.handleSmoothAndWhiteSkin(bitmap, smoothValue, skinValue);
                emitter.onNext(bitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) {
                    return;
                }
                mSaveBmp = bitmap;
                mImgEditActivity.mImgView.setImageBitmap(mSaveBmp);
            }
        });
    }

}